In this video, I'm going to introduce event handling and I'm going to connect my click detector right
here that we're going to create to a make steps function.
So when we click it, boom, we make steps.
That's pretty cool.
And you can click it again and make more steps.
And I do not have a limit on it, but I do have a D bounce.
So while the steps are being made, it can't be pressed.
Again, I want to show you that too.
So let's go ahead and get started with that.
All right.
So this is a world from my last video.
It only has a single script in it.
And the script is to make steps.
We can take a look at it real quick.
I will put it in the description in case you didn't see the last video.
But we do need a function and something to handle our click event.
So I thought this would be good.
All right, so we have a function to make steps.
What we don't have is a function to call in order to create all the steps.
We just have this loose code here.
We have a for loop that creates a bunch of steps.
All right, we can fix that.
Let's go back to our world and make our button.
Right.
So we are going down the Z axis on our steps.
Let's go ahead and face this way to make our click detector go to part out of block.
Here's my block.
I'll make it a little bigger.
This is going to be the stand that holds the button.
Always just make it a little bit bigger like that.
That's pretty good.
Cool.
Let's call this stand and we've got to make sure that it's anchored.
You can color it if you want, but we don't want it to fall over anchored.
Cool.
Now I want to put a button on that.
So I don't want to be facing out here.
I want the button to fly out here.
I probably should have told you that earlier.
You probably figured it out by now.
I'm going to go ahead and hit a cylinder.
This is going to be my button.
I'm going to call it button.
I know it doesn't look like a button, but we will fix that.
There we go.
Now I'm going to make that button green.
Brick color, really green so I can see it.
Lime green.
And then let's go down to the size.
It's too long, right?
See that red?
That means it's on the x axis.
And you could see that here for one one.
I'm going to make it 0.211 for the size.
There we go.
It's nice and thin.
Don't make it too small or your phone.
People are never going to be able to click on it.
All right.
Let's go to rotate and let's make it like that.
I'm going to have it centered on my my my stand.
So I'm turning collisions off.
And then I'm going to go to stand.
I'm going to go position control C to copy the position, get my button control V to paste the position
of the stand into the button now directly in the center.
That's why I turn collisions off when I hit the move.
Drag that out, hit the collisions.
Bump that up against.
If you push too hard, it'll go through, but you'll feel a bump, right?
And we can move this up a little bit.
That's pretty cool.
That's our button.
And we do have it anchored, right?
Oh, we do not anchor it so it doesn't fall.
If it falls down, it's not anchored.
All right, so we got our stand.
I'm going to hit a control.
Click on the button control.
Click on the steps.
This is everything we need for our make steps.
Let's right click and then group.
Cool.
Now I'm going to call this.
It's a model, right?
It's a it's a subcategory of folder.
It's a specialized folder.
I'm going to call this steps.
That way when we open steps up, we see all of our stuff in there.
Nice little package.
Cool.
Let's go ahead and open steps up.
Oh, you know what?
We've got to put a click detector on our button.
Get our button plus click detector.
There we go.
That would have been bad.
We would have forgot it.
We would have been confused.
Let's do steps.
Open that up.
Cool.
Let's get a reference for our click detector.
It is inside the model.
But we can do that.
We can get that.
I'm going to call it click.
You can call the button, whatever makes sense.
And then it's script dot parent, right?
That takes us to the model level.
Then we got to go down a level to button, dot button, and then down another level to click detector.
This takes a little while to get used to this type of of moving around, trying to get different components.
But once you get it, it won't be too bad.
Cool.
We got our click detector.
We can get rid of this.
Wait, because we're going to fire off our make steps, which is essentially going to be this inside
a function using an event handler.
Right.
When we click the button so we don't have to wait to get into the world.
Let's make a function right here, say a local function.
I'm going to call this make steps.
So the first function was make step.
Without the SW, this is going to make a whole bunch of steps.
Right?
I'm going to come down here, I'm going to put another end and my formatting is all messed up.
So I'll go to format selection.
I'm under script format, selection, format, document.
Ooh, look at that.
All of my code blocks are formatted correctly.
Cool beans.
Now we need to call this right.
So I'm going to go down below where I want to call it.
I'm going to get my click.
Remember, that's my click detector dot.
Oh, see that lightning bolt?
That means it's an event, right?
And that's universal across a lot of different environments, not just road blocks.
You'll see that in other things too.
Like, you know, when you're doing C-sharp programming, they show your events like that.
Let's double click it, bam, cool, hit a colon, connect, make steps.
If your ID, if you're if your environment puts parentheses in here like this, you got to get rid of
them when you call the function.
Via a connector from an event.
You only put the function name in there.
All right.
And we'll do more with arguments later.
Just know that you can call this from here.
Just the name, not the parentheses.
All right, we could try this out.
We could still spam it, though.
We don't have anybody anything limiting how many times you hit this button or how fast.
Let's go to view output.
Hit that all we're making or making steps.
Cool.
We can make more steps.
Cool, but we can spam it.
See, has it going.
One, two, three, four.
Watch this.
Boom, boom, boom, boom, boom, boom, boom.
So you got a lot of different numbers here.
That's crazy, right?
You got a couple nines.
You got ten here, nine there.
You'll crash your machine.
Kids will discover discover how to do that, and then you'll be out of luck.
So we got to put a bounce in there.
I'm going to hit this just to get some more space, turn my game off.
And a bounce is something that limits how many times code could be called.
So that's what we're going to do.
We're going to do a bounce.
It was not invented in robots.
It was invented a long time ago.
It was for electronic circuits, actually for the switches, so that it wouldn't fire multiple times.
Let's go ahead and make a can make four can make steps.
And we're going to say true, this is going to be my flag saying whether we can make the steps or not
can make.
We're going to initially set it to true.
We're going to come down here to our make steps and we are going to do a conditional statement.
We're going to do an if statement, right.
When I say if can make then right?
So so this is this will execute if our condition is true, right?
It is true.
Now can make is equal to true.
Let's go out and get our for loop control x pasted inside here.
Awesome.
I am going to move this end up a little bit so it's a little neater and when we do can make what we
want to do is turn can make to false, which is going to prevent this from firing again until we turn
it true.
Where do we do that?
Right underneath the for loop.
We might want to wait even a second until it's done before we can make more steps.
Then we're going to say can make equals true.
So it'll be false until we're finished making the steps and then we'll make it true.
And you might even want to put a limit to how many times you can loop it.
You're going to learn plenty about that in later videos.
Let's hit our play.
And we'll take a look we'll take a look at our output window just to see if we've got errors.
All right.
It makes our steps.
Can we spam?
I'm going to try it.
Click, click, click, click, click, click.
Look at that.
It's not acting all crazy, but it does start making them again after that one second delay or two second
delay.
But you see, they're all in order.
One, two, three, four, and we'll go from 1 to 10.
We could see how many parts are in the world here.
So that's a lot, right?
I'm going to introduce the debris service soon enough.
But for now, you can make steps, you can capture a click event and then hook it up to a function connected
to a function.
So I will see you in the next video.